package com.pure.view
{
	import com.pure.ApplicationFacade;
	import com.pure.model.UserProxy;
	import com.pure.util.MvcEvent;
	import com.pure.view.components.UserList;
	
	import flash.events.Event;
	
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class UserListMediator extends Mediator
	{
		public static const NAME:String = "UserListMediator";
		
		private var userProxy:UserProxy;
		
		public function UserListMediator(viewComponent:UserList)
		{
			super(NAME, viewComponent);
		}
		
		override public function onRegister():void
		{
			// listen the event of the view
			userList.addEventListener( MvcEvent.EDIT, onEdit);
			userList.addEventListener( MvcEvent.CREATE, onCreate);
			userList.addEventListener( MvcEvent.DELETE, onDelete);
			
			userProxy = UserProxy( facade.retrieveProxy( UserProxy.NAME ) );
			userList.users = userProxy.users;
		}
		
		private function get userList():UserList
		{
			return viewComponent as UserList;	// viewComponent is declear in Mediator
		}
		
		/**
		 * ********* functions for help *******************
		 */
		private function onEdit(event:Event):void
		{
			//communicate with the UserFormMediator
			sendNotification( ApplicationFacade.EDIT_USER, userList.userDataGrid.selectedItem );
		}
		
		private function onCreate(event:Event):void
		{
			//communicate with the UserFormMediator
			sendNotification( ApplicationFacade.CREATE_USER);
		}
		
		private function onDelete(event:Event):void
		{
			sendNotification( ApplicationFacade.CMD_DEL_USER, userList.userDataGrid.selectedItem);
		}
	}
}